package com.uplift.mgt.building.service.impl;

import com.github.pagehelper.PageHelper;
import com.uplift.common.Const;
import com.uplift.common.base.ServiceReslut;
import com.uplift.mgt.building.dao.BuildingMapper;
import com.uplift.mgt.building.dao.RealtyAdMapper;
import com.uplift.mgt.building.model.BuildingModel;
import com.uplift.mgt.building.model.RealtyAdModel;
import com.uplift.mgt.building.service.IBuildingService;
import com.uplift.mgt.building.service.IRealtyAdService;
import com.uplift.sec.SecurityContext;
import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class RealtyAdServiceImpl implements IRealtyAdService {

	@Autowired
	private RealtyAdMapper realtyAdMapper;

	@Override
	public List<Map<String, Object>> queryRealtyAd(RealtyAdModel realtyAdModel) {
		PageHelper.startPage(realtyAdModel.getPage(), realtyAdModel.getLimit());
		return this.realtyAdMapper.selectRealtyAd(realtyAdModel);
	}

	@Override
	public ServiceReslut addRealtyAd(RealtyAdModel realtyAdModel) throws Exception {
		Map<String, Object> sessionUser = SecurityContext.getSessionUser();
		if(sessionUser == null) {
			return ServiceReslut.newServiceReslut(0, "获取登录用户信息失败");
		}
		CommonsMultipartFile cmf = realtyAdModel.getFile();

		if(cmf == null) {
			return ServiceReslut.newServiceReslut(0, "广告图片为空");
		}

		FileItem item = cmf.getFileItem();
		String fileName = item.getName();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;
		String newFilePath = "product/" + newFileName;
		File file = new File(Const.DIR_UPLOAD_BASE + newFilePath);
		item.write(file);
		// 设置图片路径
		realtyAdModel.setPicturePath(newFilePath);

		realtyAdModel.setCreateUserId((String) sessionUser.get("id"));
		realtyAdModel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		realtyAdModel.setState("1");
		realtyAdModel.setCreateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyAdMapper.insertRealtyAd(realtyAdModel), "");
	}

	@Override
	public ServiceReslut updateRealtyAd(RealtyAdModel realtyAdModel) throws Exception {
		CommonsMultipartFile cmf = realtyAdModel.getFile();

		if(cmf == null) {
			return ServiceReslut.newServiceReslut(0, "广告图片为空");
		}

		FileItem item = cmf.getFileItem();
		String fileName = item.getName();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;
		String newFilePath = "product/" + newFileName;
		File file = new File(Const.DIR_UPLOAD_BASE + newFilePath);
		item.write(file);
		// 设置图片路径
		realtyAdModel.setPicturePath(newFilePath);

		realtyAdModel.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyAdMapper.updateRealtyAd(realtyAdModel), "");
	}

	@Override
	public ServiceReslut deleteRealtyAd(RealtyAdModel realtyAdModel) {
		realtyAdModel.setState("0");		//删除状态
		realtyAdModel.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyAdMapper.updateRealtyAd(realtyAdModel), "");
	}

}
